//
//  ImageScanView.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/30.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ImageScanView.h"

@interface ImageScanView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scroView;
@property(nonatomic,strong)NSMutableArray * images;
@property(nonatomic,strong)UILabel * indexTitle;
@property(nonatomic,assign)NSInteger currentIndex;
@property(nonatomic,strong)NSMutableArray * ViewArray;
@property(nonatomic,strong)NSArray * imageArray;
@end

@implementation ImageScanView

- (instancetype)initWithFrame:(CGRect)frame Images:(NSArray *)images CurrentInden:(NSInteger)currentIndex{
    self = [super initWithFrame:frame];
    if (self) {
        _images = [images mutableCopy];
        _currentIndex = currentIndex;
        _imageArray = images;
        _ViewArray = [NSMutableArray array];
        self.backgroundColor = [UIColor colorWithWhite:0.239 alpha:1.000];
        _scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _scroView.contentSize = CGSizeMake(frame.size.width*images.count, frame.size.height);
        _scroView.showsHorizontalScrollIndicator = NO;
        _scroView.pagingEnabled = YES;
        _scroView.delegate = self;
        
        [self addSubview:_scroView];
        
        _indexTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height-45*VerticalRatio(), frame.size.width, 45*VerticalRatio())];
        _indexTitle.textAlignment = NSTextAlignmentCenter;
        _indexTitle.textColor = [UIColor whiteColor];
        _indexTitle.font = [UIFont systemFontOfSize:17*VerticalTexeRatio()];
        _indexTitle.text = [NSString stringWithFormat:@"%ld/%ld",currentIndex+1,images.count];
        [self addSubview:_indexTitle];
        
        
       
       
        for (int i = 0; i<images.count; i++) {
            
            
            UIScrollView * scroView = [[UIScrollView alloc]initWithFrame:flexibleFrame(CGRectMake(frame.size.width*i, 0, frame.size.width, frame.size.height), YES)];
            scroView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
            scroView.showsHorizontalScrollIndicator = NO;
            scroView.showsVerticalScrollIndicator = NO;
            scroView.minimumZoomScale = 0.5;
            scroView.maximumZoomScale = 2;
            scroView.delegate = self;
            
            UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap)];
            [doubleTap setNumberOfTapsRequired:2];
            [scroView addGestureRecognizer:doubleTap];
            
            
            
            UIImage * image = images[i];
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, image.size.width, image.size.height), YES)];
            imageView.center = CGPointMake(scroView.frame.size.width/2, scroView.frame.size.height/2);
            imageView.image = image;
            [scroView addSubview:imageView];
            
            [_ViewArray addObject:scroView];
            [_scroView addSubview:scroView];
            
        }
        
        [self scrollToViewCenter];
        
       

    }
    return self;
}



- (void)handleDoubleTap{
    UIScrollView * scroView = _ViewArray[_currentIndex];
    UIImageView * imageView = [[UIImageView alloc]init];
    for (UIView * view in scroView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            imageView = (UIImageView *)view;
        }
    }
    UIImage * image = _imageArray[_currentIndex];
    [UIView animateWithDuration:0.2 animations:^{
        if (imageView.frame.size.width == image.size.width) {
            imageView.frame = flexibleFrame(CGRectMake(0, 0, image.size.width*2, image.size.height*2), YES);
            imageView.center = CGPointMake(scroView.frame.size.width/2, scroView.frame.size.height/2);
        }else{
            imageView.frame = flexibleFrame(CGRectMake(0, 0, image.size.width, image.size.height), YES);
            imageView.center = CGPointMake(scroView.frame.size.width/2, scroView.frame.size.height/2);
        }
      
    }];
    
}



- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    if (scrollView!=_scroView) {
        UIScrollView * scroView = _ViewArray[_currentIndex];
        UIImageView * imageView = [[UIImageView alloc]init];
        for (UIView * view in scroView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                   imageView = (UIImageView *)view;
            }
        }
        return imageView;
    }
    return nil;
}



- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    if (scrollView!=_scroView) {
        UIScrollView * scroView = _ViewArray[_currentIndex];
        UIImageView * imageView = [[UIImageView alloc]init];
        for (UIView * view in scroView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                imageView = (UIImageView *)view;
            }
        }
        imageView.center = CGPointMake(scroView.frame.size.width/2, scroView.frame.size.height/2);
    }

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scroView) {
   
            CGPoint offset = scrollView.contentOffset;
            _currentIndex = offset.x / (self.frame.size.width); //计算当前的页码

    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == _scroView) {
        [self scrollToViewCenter];
    }
}

- (void)scrollToViewCenter {
    [_scroView setContentOffset:CGPointMake(self.frame.size.width * _currentIndex,_scroView.contentOffset.y) animated:YES]; //设置scrollview的显示为当前滑动到的页面
    _indexTitle.text = [NSString stringWithFormat:@"%ld/%ld",_currentIndex+1,_images.count];
  
}



@end
